package com.fwmagic.rpc.provider;

import com.fwmagic.rpc.handler.ProcessHandler;
import com.fwmagic.rpc.service.UserServiceImpl;
import com.fwmagic.rpc.service.service.IUserService;
import sun.java2d.loops.ProcessPath;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class RpcServerProvider {

    /**定义一个线程池*/
    ExecutorService executorService = Executors.newCachedThreadPool();

    public void publisher(Object service,int port) {
        ServerSocket serverSocket = null;
        try {
            //Socket绑定本地端口
            serverSocket = new ServerSocket(port);
            System.err.println("The ServerSocket start in port:"+port+"!!!");
            //监听端口
            while (true) {
                Socket socket = serverSocket.accept();
                executorService.execute(new ProcessHandler(socket,service));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (serverSocket != null) {
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
